package com.ideal.frame.util;

import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ShellUtil {

    public static void runShell(List<String> params,String shellPath) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(shellPath);
        params.forEach(param -> {
            stringBuilder.append("\t" + param);
        });

        Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",stringBuilder.toString()});
        System.out.println("命令为 ---------"+stringBuilder.toString());
        process.waitFor();

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        String result = sb.toString();
        log.info(result);

        int exitCode = process.exitValue();

        Assert.isTrue(exitCode == 0,"shell执行失败");
    }


    public static void main(String[] args) throws Exception {
        List<String> params = new ArrayList<>();
        String keyStoreDir = "/home/dingshengliu/Desktop/gly/标识ma流程设计/assignKeyStore/";
        String trustStore = "/home/dingshengliu/Desktop/gly/标识ma流程设计/trustStore/cacerts";
        params.add(keyStoreDir + "123.keystore");
        params.add("127.0.0.1");
        params.add(trustStore);
        runShell(params,"/home/dingshengliu/Desktop/gly/标识ma流程设计/createCer.sh");
    }


}
